package com.slashmanx.socialwall;

import com.slashmanx.helpers.Utilities;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class SocialWallService extends Service implements OnSharedPreferenceChangeListener
{
	private static final String TAG = "SocialWallService";
	public static final String BROADCAST_ACTION = "com.slashmanx.socialwall.downwall";
	private final Handler handler = new Handler();
	Intent intent;
	SharedPreferences sharedPrefs;



	@Override
	public IBinder onBind(Intent arg0) 
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();

    	intent = new Intent(BROADCAST_ACTION);	
    	sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
    	sharedPrefs.registerOnSharedPreferenceChangeListener(this);
	}


    @Override
    public void onStart(Intent intent, int startId) {
        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
   
    }
    
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
    	if(key.equals("updateInterval"))
    	{
	    	handler.removeCallbacks(sendUpdatesToUI);
	    	handler.postDelayed(sendUpdatesToUI, (sharedPreferences.getInt("updateInterval", 15) * 1000 * 60));
    	}
    }
    
    private Runnable sendUpdatesToUI = new Runnable() {
    	public void run() {
    		if(Utilities.isOnline(getBaseContext()))
    		{
    			doUpdate();
    		}

			handler.postDelayed(this, (sharedPrefs.getInt("updateInterval", 15) * 1000 * 60)); // 15 mins
    	}
    };    
    
    private void doUpdate() {
    	Log.d(TAG, "entered doUpdate()");
    	sendBroadcast(intent);
    }

    
	@Override
	public void onDestroy() {		
        handler.removeCallbacks(sendUpdatesToUI);		
		super.onDestroy();
	}		

/*
 package com.slashmanx.goals2u;


import com.slashmanx.goals2u.others.Utilities;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class GoalsService  extends Service {
	private static final String TAG = "GoalsService";
	public static final String BROADCAST_ACTION = "com.slashmanx.goals2u.displayevent";
	private final Handler handler = new Handler();
	Intent intent;
	int counter = 0;
	SharedPreferences sharedPrefs;
	@Override
	public void onCreate() {
		super.onCreate();

    	intent = new Intent(BROADCAST_ACTION);	
    	sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
	}

    @Override
    public void onStart(Intent intent, int startId) {
        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
   
    }

    private Runnable sendUpdatesToUI = new Runnable() {
    	public void run() {
    		if(Utilities.isOnline(getBaseContext()))
    		{
    			doUpdate();
    		}

			handler.postDelayed(this, (sharedPrefs.getInt("refreshInterval", 10) * 1000)); // 10 seconds
    	}
    };    
    
    private void doUpdate() {
    	Log.d(TAG, "entered doUpdate()");
    	sendBroadcast(intent);
    }

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {		
        handler.removeCallbacks(sendUpdatesToUI);		
		super.onDestroy();
	}		
}

 */
}
